package com.qf.dao.impl;

import com.qf.dao.HealthDao;
import com.qf.entity.Health;
import com.qf.entity.Organ;
import com.qf.util.DruidUtil;
import com.qf.util.Page;
import com.sun.deploy.net.HttpRequest;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.beans.EventHandler;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*
杨威
2021/10/5
15:09
*/
public class HealthDaoImpl implements HealthDao {
    QueryRunner queryRunner=new QueryRunner();
    @Override
    public List<Health> selectAll(Page page) {
        try {
            String sql="select health.id,projectname,status,deadline,money,oid,organname from health\n" +
                    "inner join organ\n" +
                    "on health.oid=organ.id limit ?,?;";
            List<Map<String, Object>> query = queryRunner.query(DruidUtil.getConnection(), sql, new MapListHandler(), page.getStartRow(), page.getPageSize());
            List list=new ArrayList();
            for (Map<String, Object> map : query) {
                Health health=new Health();
                Organ organ=new Organ();
                BeanUtils.populate(health,map);
                BeanUtils.populate(organ,map);
                health.setOrgan(organ);
                list.add(health);
            }
            return list;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }
    public Long selectCount(){
        try {
            String sql="select count(*) from health";
            Long query = queryRunner.query(DruidUtil.getConnection(), sql, new ScalarHandler<>());
            return query;
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public int insert(Health health) {
        try {
            String sql="insert into health values(?,?,?,?,?,?)";
            Object[] params={health.getId(),health.getProjectname(),health.getStatus(),health.getDeadline(),health.getMoney(),health.getOrgan().getOid()};
            int update = queryRunner.update(DruidUtil.getConnection(), sql, params);
            return update;
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public Health select(Integer id) {
        try {
            String sql="select * from health where id=?";
            Health health = queryRunner.query(DruidUtil.getConnection(), sql, new BeanHandler<>(Health.class), id);
            return health;
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public int update(Health health) {
        try {
            String sql="update health set projectname=?,status=?,deadline=?,money=?,oid=? where id=?";
            Object[] params={health.getProjectname(),health.getStatus(),health.getDeadline(),health.getMoney(),health.getOrgan().getOid(),health.getId()};
            int update = queryRunner.update(DruidUtil.getConnection(), sql, params);
            return update;
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public int delete(Integer id) {
        try {
            String sql="delete from health where id=? ";
            int update = queryRunner.update(DruidUtil.getConnection(), sql, id);
            return update;
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage());
        }
    }
}
